package javabasic.threads.CreateThreadMethods;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 线程池提交 实现runnable接口的任务实例
 */
public class Method05PoolRunnable {
    public static void main(String[] args) {
        RunnablePool runnablePool = new RunnablePool();
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        Future<?> future = cachedThreadPool.submit(runnablePool);
        while (!future.isDone()){
            System.out.println("子线程未执行完毕，请等待。。。。。。");
            try {
                //增加等待是为了看到在控制台的输出，不然输出可能会被缓冲控制台打印看不出来子线程是否执行
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"====="+i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        cachedThreadPool.shutdown();
    }
}

class RunnablePool implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"------"+i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
